﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEPMserver_BLL;
using WEPMlibrary_Model;
using DotNet.Utilities;
using System.IO;
using System.Text;
using PageLibrary1;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        JurisdictionBll jbll = new JurisdictionBll();
        ReviewBLL rbll = new ReviewBLL();
        Project_BLL pbll = new Project_BLL();
        Member_BLL mbll = new Member_BLL();
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int Login(string name, string pwd)
        {
            List<Jurisdiction> list = jbll.JurisdictionShow(name, pwd);
            if (list != null && list.Count > 0)
            {
                Session["name"] = name;
                Session["pwd"] = pwd;
                return 1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 登录RBAC
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public string NavShow()
        {
            List<Jurisdiction> list = jbll.JurisdictionShow(Session["name"].ToString(), Session["pwd"].ToString());
            if (list != null && list.Count >0)
            {
                string menuHTML = "<dl>";
                foreach (var item in list)
                {
                    if (item.ParentNodeId == 0)
                    {
                        //拼接父级菜单
                        menuHTML += "<dt><span>" + item.JurisdName + "</span></dt><dd>";
                        foreach (var child in list)
                        {
                            if (child.ParentNodeId != 0)
                            {
                                menuHTML += "<a href=" + child.JurisdUrl + " title=" + child.JurisdName + ">" + child.JurisdName + "</a>";
                            }
                            else
                            {
                                continue;
                            }

                        }
                        menuHTML += "</dd>";
                    }
                    else
                    {
                        continue;
                    }

                }
                menuHTML += "</dl>";

                return menuHTML;
                
            }
            else
            {
                return "";
            }
            
        }
        public ActionResult Shows()
        {
            return View();
        }
        /// <summary>
        /// 文件上传页面
        /// </summary>
        /// <returns></returns>
        public ActionResult fileup()
        {
            return View();
        }
        /// <summary>
        /// 上传文件的显示页面（包含修改等）
        /// </summary>
        /// <returns></returns>  
        public ActionResult fileupdate()
        {
            return View();
        }
        /// <summary>
        /// 分页显示文件
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public ActionResult showfile(int index, int size)
        {
            FileBll bll=new FileBll ();
            var uid = bll.findid(Session["name"].ToString());
            var duty = bll.duty(uid);
            Page<WEPMlibrary_Model.File> list = new FileBll().pageList(index, size,duty);
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public ActionResult upfile()
        {
            FileBll bll = new FileBll();
            var a = Request.Files[0];
            var pathname = a.FileName;
            string txtString = string.Empty;
            WEPMlibrary_Model.File files = new WEPMlibrary_Model.File();
            //上传人id
            var uid = bll.findid(Session["name"].ToString());
            //类型
             files.FileTyep=  bll.duty(uid);
           
            //项目编号
            files.ProjectID = 1;
            //文件状态
            files.fileState = "未审核";
            files.Uid =uid;
            //上传时间
            files.fileDate = DateTime.Now.ToString("yyyy/MM/dd");
            //上传到项目
            FileUp file = new FileUp();
            byte[] by = file.GetBinaryFile(pathname);
            var name = pathname.Split('\\');
            for (var i = 0; i < name.Length; i++)
            {
                if (name[i].Contains("."))
                {
                    var name1 = name[i].Split('.');
                    if (name1[1] == "txt" || name1[1] == "docx" || name1[1] == "jpg" || name1[1] == "png")
                    {
                        file.SaveFile(by, "" + name1[0] + "", "." + name1[1] + "");
                        var date = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        var paname=" "+ (name1[0]+date) + "." + name1[1];
                        //文件名
                        files.filename=paname;

                        string filePath = Server.MapPath("../File/" + paname);
                        //保存文件
                        a.SaveAs(filePath);
                        //读文件
                        StreamReader sr = new StreamReader(filePath, Encoding.Default);
                        //读到的文件内容
                        txtString = sr.ReadToEnd();
                        sr.Dispose();
                    }
                    else
                    {
                        return Content("格式不支持");
                    }

                }
            }
            FileBll fbll = new FileBll();
            if (fbll.addfile(files) > 0)
            {
               // return Content(txtString);
                return Content("上传成功");
            }
            else
            {
                return Content("上传失败");
            }
           
        }
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        public ActionResult downfile()
        {
            //var pathname = Request["name"];
           // var pathname = "/File/20161205074551.mkv";
            var pathname = Request.QueryString["filepath"];
             FileDown.DownLoad("/File/"+pathname+"");
            return Content("下载成功");
        }
        //内审-----------------------------------
        /// <summary>
        /// 项目添加
        /// </summary>
        /// <returns></returns>
        public ActionResult AddProject()
        { 
            return View();
        }
        /// <summary>
        /// 添加功能
        /// </summary>
        /// <returns></returns>
        public int AddDoProject(Project p)
        {
            FileBll bll = new FileBll();
            //创建时间
            p.DeclareTime = DateTime.Now.ToString("yyyy/MM/dd");
            //建项目的人
            var uid = bll.findid(Session["name"].ToString());
            p.Uid = uid;
            if (pbll.ProjectAdd(p) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 项目显示
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowProject()
        {
            return View();
        }
        /// <summary>
        /// 项目显示功能
        /// </summary>
        /// <returns></returns>
        public JsonResult ProJectShows(int index, int size)
        {
            Page<Project> list = pbll.ProjectShow(index,size);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
       /// <summary>
       /// 添加成员
       /// </summary>
       /// <returns></returns>
        public ActionResult AddMember()
        {
            return View();
        }
        /// <summary>
        /// 项目下拉框显示
        /// </summary>
        /// <returns></returns>
        public JsonResult ProjectDrop()
        { 
            List<Project> list=pbll.MemberAdd();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 显示要赋予权限的所有用户职责
        /// </summary>
        /// <returns></returns>
        public JsonResult UserShows()
        {
            List<UserInfor> list = pbll.UserInforShow();
            return Json(list, JsonRequestBehavior.AllowGet); 
        }
        public int AddMemberTypes()
        {
            int projectid = Convert.ToInt32(Request["projectid"]);
            int lixiang=Convert.ToInt32(Request["lixiang"]);
            int fangan=Convert.ToInt32(Request["fangan"]);
            int chengben=Convert.ToInt32(Request["chengben"]);
            int caiwu=Convert.ToInt32(Request["caiwu"]);
            int gongcheng=Convert.ToInt32(Request["gongcheng"]);
            int lixiangid=Convert.ToInt32(Request["lixiangid"]);
            int fanganid=Convert.ToInt32(Request["fanganid"]);
            int chengbenid=Convert.ToInt32(Request["chengbenid"]);
            int caiwuid=Convert.ToInt32(Request["caiwuid"]);
            int gongchengid=Convert.ToInt32(Request["gongchengid"]);
            var types = "";
            var uid=0;
            var pid=0;
            int result=0;
            if (lixiang == 1)
            {
                types = "立项负责人";
                uid = lixiangid;
                pid = projectid;
                result = pbll.AddProjectmen(types, uid, pid);
                if (result > 0)
                {
                    if (fangan == 2)
                    {
                        types = "方案负责人";
                        uid = fanganid;
                        pid = projectid;
                        result = pbll.AddProjectmen(types, uid, pid);
                        if (result > 0)
                        {
                            if (chengben == 3)
                            {
                                types = "成本负责人";
                                uid = chengbenid;
                                pid = projectid;
                                result = pbll.AddProjectmen(types, uid, pid);
                                if (result > 0)
                                {
                                    if (caiwu == 4)
                                    {
                                        types = "财务负责人";
                                        uid = caiwuid;
                                        pid = projectid;
                                        result = pbll.AddProjectmen(types, uid, pid);
                                        if (result > 0)
                                        {
                                            if (gongcheng == 5)
                                            {
                                                types = "工程负责人";
                                                uid = gongchengid;
                                                pid = projectid;
                                                result = pbll.AddProjectmen(types, uid, pid);
                                                if (result > 0)
                                                {
                                                    return 1;
                                                }
                                                else
                                                {
                                                    return 0;
                                                }
                                            }
                                            else
                                            {
                                                return 0;
                                            }
                                        }
                                        else
                                        {
                                            return 0;
                                        }
                                    }
                                    else
                                    {
                                        return 0;
                                    }
                                }
                                else
                                {
                                    return 0;
                                }
                            }
                            else
                            {
                                return 0;
                            }
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        //内审-----------------------------------
        /// <summary>
        /// 查看项目
        /// </summary>
        /// <returns></returns>
        public ActionResult Project()
        {
            return View();
        }
        ///// <summary>
        ///// 显示项目信息
        ///// </summary>
        ///// <returns></returns>
        //public JsonResult ShowProject()
        //{
        //    List<Project> list = rbll.ShowProject();
        //    return Json(list, JsonRequestBehavior.AllowGet);
        //}
        /// <summary>
        /// 团队信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Team()
        {
            return View();
        }
        public JsonResult Team_Show()
        {
            List<Member> list = mbll.Team();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <returns></returns>
        public ActionResult Downloading()
        {
            return View();
        }
        /// <summary>
        /// 上传意见书
        /// </summary>
        /// <returns></returns>
        public ActionResult Uploading()
        {
            return View();
        }
        /// <summary>
        /// 通知评委会
        /// </summary>
        /// <returns></returns>
        public ActionResult Notice()
        {
            return View();
        }

        /// <summary>
        /// 添加项目负责人功能
        /// </summary>
        /// <returns></returns>
        public int AddDoMember(Member m)
        {
            if (pbll.MemberAdd(m) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        
    
        /// <summary>
        /// 评审会  查看信息页面
        /// </summary>
        /// <returns></returns>
        public ActionResult LookUp()
        {
            return View();
    }
        /// <summary>
        /// 显示消息信息
        /// </summary>
        /// <returns></returns>
        public JsonResult ShowMessage()
        {
            List<Message> list = rbll.ShowMessage();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult DownLoad()
        {
            return View();
        }
        /// <summary>
        /// 评审会   显示下载相关文件
        /// </summary>
        /// <returns></returns>
        public JsonResult ShowDownLoad()
        {
            ReviewBLL bll = new ReviewBLL();
            List<Project> list = bll.ShowProject();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 评审会   下载相关文件试图
        /// </summary>
        /// <returns></returns>
        public ActionResult InforMation()
        {
            return View();
        }
}
}
                            